package com.alibaba.service;

import com.alibaba.common.ResponseResult;
import com.alibaba.define.domain.CategoryDO;
import com.alibaba.define.domain.vo.CategoryVO;
import com.alibaba.define.service.CategoryService;
import com.alibaba.mapper.ArticleMapper;
import com.alibaba.mapper.CategoryMapper;
import com.alibaba.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.alibaba.define.enums.AppHttpCodeEnum.SYSTEM_EXECUTE_ERROR;

/**
 * @author : hanyang
 * @date : 2023-10-30 23:12
 **/

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryDAO;

    @Resource
    ArticleMapper articleDAO;

    /**
     * 查询所有已发布文章的类型名
     */
    @Override
    public ResponseResult<List<CategoryVO>> queryNameList() {
        // 查询所有已发布文章的类别id
        try {
            List<Long> categoryIds = articleDAO.selectArticleCategory();
            List<CategoryDO> categoryDOS = categoryIds.stream().map(aLong -> categoryDAO.selectById(aLong)).collect(Collectors.toList());
            List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(categoryDOS, CategoryVO.class);
            return ResponseResult.ok(categoryVOS);
        } catch (Exception e) {
            log.error("[CategoryService] query categoryName list error!", e);
        }
        return ResponseResult.error(SYSTEM_EXECUTE_ERROR.getCode(), SYSTEM_EXECUTE_ERROR.getMsg());
    }
}
